<template>
    <div class="goodslist-containwe">

        <!-- <router-link class="goodslist-show" v-for="item in goodsList" :key="item.id"
        :to='"/home/goodsInfo/"+item.id'> -->
        
        <!-- 在网页中，有两种跳转方式： -->
        <!-- 方式1：使用a标签的形式 叫做标签跳转 -->
        <!-- 方式2：使用window.localtion.href 的形式，叫做编程式导航 -->
        <div class="goodslist-show" v-for="item in goodsList" :key="item.id" @click="goDatail(item.id)">
            <img :src="item.img_url" alt="">
            <h3>{{item.title}}</h3>
            <div class="info">
                <p class="goods-price">
                <span class="new">￥{{item.sell_price}}</span>
                <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="goods-sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>           
        <mt-button type="danger" size="large" @click="getmore">加载更多</mt-button>
    </div>
</template>
<script>
import { Toast } from "mint-ui";
export default {
    data(){
        return{
            pageindex:1, // 分页的页数
            goodsList:[]// 存放商品列表的数组
        }
    },
    created(){
        this.getgoodsList()
    },
    methods:{
        getgoodsList(){
            // 获取商品列表
            this.$http.get('api/getgoods?pageindex='+this.pageindex).then(result =>{
                if(result.body.status === 0){
                    this.goodsList = this.goodsList.concat(result.body.message)
                }
            })
        },
        getmore(){
            this.pageindex++
            this.getgoodsList()
        },
        goDatail(id){
            // 使用JS的形式进行路由导航
            
            //注意：一定要区分this.$route和this.$router这两个对象
            // 其中：this.$route是路由【参数对象】，所有路由中的参数，params,query都属于它
            // console.log(this)
            
            // 1.最简单
            // this.$router.push('/home/goodsInfo/'+id)
            // 2.传递对象
            // this.$router.push({ path: '/home/goodsInfo/'+id })
            // 3.传递命名的路由
            this.$router.push({ name: 'goodsinfo', params: { id }})
            // 4.传递带查询参数，变成 /register?plan=private
            // this.$router.push({ path: 'register', query: { plan: 'private' }})

        }
    }
    
}
</script>
<style lang="scss" scoped>
.goodslist-containwe{
    display: flex;
    flex-wrap: wrap;
    padding: 6px;
    justify-content: space-between;
    .goodslist-show{
        width: 49%;
        border: 1px solid #ccc;
        box-shadow: 0 0 4px #ccc;
        margin: 3px 0;
        padding: 2px;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-height: 294px; 
        img{
            width: 100%;
        }
        h3{
            font-size: 14px;
            color: #000;
        }
        .info{
            background-color: #eee;
            p{
                margin: 0;
                padding: 5px;
            }
            .goods-price{
                .new{
                    color: #f40;
                    font-size: 16px;
                    font-weight: bold;
                    
                }
                .old{
                    text-decoration: line-through;
                    font-size: 12px;
                    margin-left: 10px;
                }
            }
            .goods-sell{
                display: flex;
                justify-content: space-between;
                font-size: 13px;
            }
        }
       
    }
}
</style>
